package cn.software.controller;


import cn.software.entity.Notes;
import cn.software.ex.NotesNotFoundException;
import cn.software.ex.NullContentException;
import cn.software.service.NotesService;
import cn.software.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notes")
public class NotesController {

    @Autowired
    NotesService notesService;

    @PostMapping("/addNotes")
    public Result addNotes(@RequestBody Notes notes) {
        try{
            boolean result =  notesService.save(notes);
            return Result.success(result);
        } catch(NullContentException e) {
            return Result.fail("输入不能为空");
        }
    }

    @GetMapping("deleteNotes")
    public Result deleteNotes(int id) {
        try{
            boolean result = notesService.removeById(id);
            return Result.success(result);
        }catch(NotesNotFoundException e) {
            return Result.fail("该笔记不存在");
        }
    }


    @PostMapping("/updateNotes")
    public Result updateNotes(@RequestBody Notes notes) {
        try{
            boolean result = notesService.updateById(notes);
            return Result.success(result);
        }catch(NotesNotFoundException e) {
            return Result.fail("更新失败");
        }
    }

    @GetMapping("selectNotes")
    public Result selectNotes(int id) {
        try{
            Notes notes = notesService.getById(id);
            return Result.success(notes);
        }catch(NotesNotFoundException e) {
            return Result.fail("查询失败");
        }
    }

    @GetMapping("selectNotesList")
    public Result selectNotesListByTopic(int topicId,int stuId) {
        try{
            List<Notes> notesList = notesService.selectNotesListByTopic(topicId, stuId);
            return Result.success(notesList);
        }catch(NullContentException e ) {
            return Result.fail("查询笔记失败");
        }
    }


}
